import { cancelOrder, deltOrder, followTarget } from '@/services/consult'
import { ref, onMounted, onUnmounted } from 'vue'
import type { Ref } from 'vue'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import { getPrescriptionPic } from '@/services/consult'
import { showImagePreview, showToast } from 'vant'
import type { FormInstance } from 'vant'
import { OrderType } from '@/enums'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import { sendMobileCode } from '@/services/user'
import type { CodeType } from '@/types/user'

export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followTarget(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { loading, follow }
}

export const usePreviewPrescription = () => {
  const showPrescription = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return { showPrescription }
}

export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = (item: ConsultOrderItem) => {
    loading.value = true
    cancelOrder(item.id)
      .then(() => {
        item.status = OrderType.ConsultCancel
        item.statusValue = '已取消'
        showToast('取消成功')
      })
      .catch(() => {
        showToast('取消失败')
      })
      .finally(() => {
        loading.value = false
      })
  }
  return { loading, cancelConsultOrder }
}

export const useDelOrder = (fn: (id: string) => void) => {
  const loading = ref(false)
  const delConsultOrder = async (item: ConsultOrderItem) => {
    loading.value = true
    try {
      await deltOrder(item.id)
      // 删除信息
      // emit('on-delete', item.id)
      fn && fn(item.id)
      showToast('删除成功')
    } catch (error) {
      showToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, delConsultOrder }
}

export const useOrderDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { loading, order }
}

export const useMobileCode = (mobile: Ref<string>, type: CodeType) => {
  const time = ref(0)
  const form = ref<FormInstance | null>(null)
  let timerId: NodeJS.Timeout
  // 发送短信
  /**
   *  1. Api接口调用函数
   *  2. 发送短信验证码 判断是否正在倒计时 校验输入框 调用短信接口
   *  3. 接口成功，倒计时，组件销毁清除
   */
  const send = async () => {
    if (time.value > 0) return
    await form.value?.validate('mobile')
    await sendMobileCode(mobile.value, type)
    time.value = 60

    // 开启倒计时
    if (timerId) clearInterval(timerId)
    timerId = setInterval(() => {
      time.value--
      if (time.value <= 0) clearInterval(timerId)
    }, 1000)
  }

  onUnmounted(() => {
    clearInterval(timerId)
  })

  return { time, form, send }
}
